package z_guestbook;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	GuestBookMain main;
	
	private JLabel label;
	private JTextField mid;
	private JLabel label_1;
	private JTextField subject;
	private JButton button;
	private JLabel lblNo;
	private JTextField serial;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTextArea content;
	private JButton btnNewButton_1;


	public Update(GuestBookMain main) {
		super("방명록 수정 및 삭제", true, true, true, true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		this.main = main;
		
		setBounds(100, 100, 544, 394);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getMid());
		getContentPane().add(getLabel_1());
		getContentPane().add(getSubject());
		getContentPane().add(getButton());
		getContentPane().add(getLblNo());
		getContentPane().add(getSerial());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton_1());

	}
	
	public void select(int s){
		try{
			String sql = "select * from guestbook where serial = ?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setInt(1, s);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				serial.setText(""+rs.getInt("serial"));
				mid.setText(rs.getString("mid"));
				subject.setText(rs.getString("subject"));
				content.setText(rs.getString("content"));
			}else{
				JOptionPane.showMessageDialog(null, "찾는 데이터가 없습니다.");
				mid.setText("");
				subject.setText("");
				content.setText("");
			}
			rs.close();
			pstmt.close();
			
		}catch(Exception ex){
			
		}
	}

	public void update(){
		String sql = "update guestbook set mid=? , subject=? , " 
				+ " content=? where serial=?";
		try{
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, mid.getText());
			pstmt.setString(2, subject.getText());
			pstmt.setString(3, content.getText());
			pstmt.setInt(4, Integer.parseInt(serial.getText()));
			
			pstmt.executeUpdate();
			
			if(main.select !=null){
				main.select.setData();
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
				
	           
	}
	
	public void delete(){
		String sql = "delete from guestbook where serial = ?";
		try{
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(serial.getText()));
			
			pstmt.executeUpdate();
			
			if(main.select != null) main.select.setData();
			
			mid.setText("");
			subject.setText("");
			content.setText("");
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("아이디");
			label.setBounds(12, 37, 57, 15);
		}
		return label;
	}
	private JTextField getMid() {
		if (mid == null) {
			mid = new JTextField();
			mid.setColumns(10);
			mid.setBounds(62, 34, 116, 21);
		}
		return mid;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("제목");
			label_1.setBounds(12, 62, 57, 15);
		}
		return label_1;
	}
	private JTextField getSubject() {
		if (subject == null) {
			subject = new JTextField();
			subject.setColumns(10);
			subject.setBounds(62, 62, 452, 21);
		}
		return subject;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					update();
				}
			});
			button.setBounds(142, 332, 97, 23);
		}
		return button;
	}
	private JLabel getLblNo() {
		if (lblNo == null) {
			lblNo = new JLabel("No");
			lblNo.setBounds(12, 12, 57, 15);
		}
		return lblNo;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(62, 9, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("삭제");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					delete();
				}
			});
			btnNewButton.setBounds(266, 332, 97, 23);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 99, 504, 218);
			scrollPane.setViewportView(getContent());
		}
		return scrollPane;
	}
	private JTextArea getContent() {
		if (content == null) {
			content = new JTextArea();
		}
		return content;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("검색");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					select(s);				
				}
			});
			btnNewButton_1.setBounds(188, 8, 97, 23);
		}
		return btnNewButton_1;
	}
}
